/**
 * Copyright (c) 2018 软唐 All rights reserved.
 * <p>
 * https://www.softtown.cn
 * <p>
 * 版权所有，侵权必究！
 */

package com.softtown.modules.sys.controller;

import com.softtown.common.annotation.LogOperation;
import com.softtown.common.constant.Constant;
import com.softtown.common.page.PageData;
import com.softtown.common.utils.Result;
import com.softtown.common.validator.AssertUtils;
import com.softtown.common.validator.ValidatorUtils;
import com.softtown.common.validator.group.DefaultGroup;
import com.softtown.common.validator.group.UpdateGroup;
import com.softtown.modules.sys.dto.SysDictDTO;
import com.softtown.modules.sys.entity.SysDictEntity;
import com.softtown.modules.sys.service.SysDictService;
import com.softtown.modules.sys.vo.SysDictVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * 数据字典
 *
 * @author Ding Ya Yong
 * @since 1.0.0
 */
@RestController
@RequestMapping("sys/dict")
@Api(tags = "数据字典")
public class SysDictController {
    @Resource
    private SysDictService sysDictService;

    @GetMapping("page")
    @ApiOperation("字典分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "dictType", value = "字典类型", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "dictName", value = "字典名称", paramType = "query", dataType = "String")
    })
    @RequiresPermissions("sys:dict:page")
    public Result<PageData<SysDictDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        //字典分类
        PageData<SysDictDTO> page = sysDictService.page(params);

        return new Result<PageData<SysDictDTO>>().ok(page);
    }

    @GetMapping("list")
    @ApiOperation("字典分类数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dictName", value = "字典名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "dictValue", value = "字典值", paramType = "query", dataType = "String")
    })
    @RequiresPermissions("sys:dict:page")
    public Result<List<SysDictDTO>> list(@ApiIgnore @RequestParam Map<String, Object> params) {
        //字典分类数据
        List<SysDictDTO> list = sysDictService.list(params);

        return new Result<List<SysDictDTO>>().ok(list);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("sys:dict:info")
    public Result<SysDictDTO> get(@PathVariable("id") Long id) {
        SysDictDTO data = sysDictService.get(id);

        return new Result<SysDictDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("sys:dict:save")
    public Result save(@RequestBody SysDictDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, DefaultGroup.class);

        sysDictService.save(dto);

        return new Result<>();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("sys:dict:update")
    public Result update(@RequestBody SysDictDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        sysDictService.update(dto);

        return new Result<>();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("sys:dict:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        sysDictService.delete(ids);

        return new Result<>();
    }

    /**
     * 查找语言种类
     *
     * @param
     * @return List<SysDictEntity>
     * @author Dingyayong
     * @date 2019/6/22 20:54
     */

    @PostMapping("getLanguageList")
    @ApiOperation("获取语言")
    public Result<List<SysDictEntity>> getLanguageList() {
        List<SysDictEntity> data = sysDictService.getLanguageList();

        return new Result<List<SysDictEntity>>().ok(data);
    }

    /**
     * 查找规则类型
     *
     * @param
     * @return List<SysDictEntity>
     * @author Dingyayong
     * @date 2019/6/22 20:54
     */
    @PostMapping("getRuleTypeList")
    @ApiOperation("获取规则类型")
    @LogOperation("获取规则类型")
    public Result<List<SysDictEntity>> getRuleTypeList() {
        List<SysDictEntity> data = sysDictService.getRuleTypeList();

        return new Result<List<SysDictEntity>>().ok(data);
    }

    /**
     * 查找规则状态
     *
     * @param
     * @return List<SysDictEntity>
     * @author Dingyayong
     * @date 2019/6/22 20:54
     */
    @PostMapping("getRuleStatusList")
    @ApiOperation("获取规则状态")
    @LogOperation("获取规则状态")
    public Result<List<SysDictEntity>> getRuleStatusList() {
        List<SysDictEntity> data = sysDictService.getRuleStatusList();

        return new Result<List<SysDictEntity>>().ok(data);
    }

    /**
     * 查找单据类型
     *
     * @param
     * @return List<SysDictEntity>
     * @author Dingyayong
     * @date 2019/6/22 20:54
     */
    @PostMapping("getDocTypeList")
    @ApiOperation("查找单据类型")
    @LogOperation("查找单据类型")
    public Result<List<SysDictEntity>> getDocTypeList() {
        return new Result<List<SysDictEntity>>().ok(sysDictService.getDocTypeList());
    }

    /**
     * @param @param []
     * @return @return com.softtown.common.utils.Result<java.util.List<com.softtown.modules.sys.vo.SysDictVO>>
     * @throws
     * @Title: getIsoCurrencyList
     * @Description: 获取出差业务币种信息
     * @author fuming
     * @date 2019/7/27 11:21
     */
    @PostMapping("getIsoCurrencyList")
    @ApiOperation("获取出差业务币种信息")
    @LogOperation("获取出差业务币种信息")
    public Result<List<SysDictVO>> getIsoCurrencyList() {
        List<SysDictVO> isoCurrCdList = sysDictService.getIsoCurrCdList();
        return new Result<List<SysDictVO>>().ok(isoCurrCdList);
    }

    /**
     * @param @param []
     * @return @return com.softtown.common.utils.Result<java.util.List<com.softtown.modules.sys.vo.SysDictVO>>
     * @throws
     * @Title: getPayMethodList
     * @Description: 获取支付方式信息
     * @author fuming
     * @date 2019/8/25 21:36
     */
    @PostMapping("getPayMethodList")
    @ApiOperation("获取支付方式信息")
    @LogOperation("获取支付方式信息")
    public Result<List<SysDictVO>> getPayMethodList() {
        List<SysDictVO> payMethodList = sysDictService.getPayMethodList();
        return new Result<List<SysDictVO>>().ok(payMethodList);
    }

    /**
     * @param @param []
     * @return @return com.softtown.common.utils.Result<java.util.List<com.softtown.modules.sys.vo.SysDictVO>>
     * @throws
     * @Title: getSupList
     * @Description: 获取出差支持需求信息
     * @author fuming
     * @date 2019/8/25 21:38
     */
    @PostMapping("getSupList")
    @ApiOperation("获取出差支持需求信息")
    @LogOperation("获取出差支持需求信息")
    public Result<List<SysDictVO>> getSupList() {
        List<SysDictVO> supList = sysDictService.getSupList();
        return new Result<List<SysDictVO>>().ok(supList);
    }

    /**
     * 获取币种信息
     *
     * @param
     * @return com.softtown.common.utils.Result<java.util.List < com.softtown.modules.sys.vo.SysDictVO>>
     * @author fuming
     * @date 2019/10/13 13:48
     */
    @PostMapping("getIsoCurrencyInfoList")
    @ApiOperation("获取币种信息")
    @LogOperation("获取币种信息")
    public Result<List<SysDictVO>> getIsoCurrencyInfoList() {
        List<SysDictVO> isoCurrCd = sysDictService.getIsoCurrCd();
        return new Result<List<SysDictVO>>().ok(isoCurrCd);
    }

    @PostMapping("getAprvMethodList")
    @ApiOperation("获取审批方式")
    @LogOperation("获取审批方式")
    public Result<List<SysDictVO>> getAprvMethodList() {
        List<SysDictVO> isoCurrCd = sysDictService.getAprvMethodList();
        return new Result<List<SysDictVO>>().ok(isoCurrCd);
    }

    /**
     * 获取借款类别
     * @param
     * @return com.softtown.common.utils.Result<java.util.List<com.softtown.modules.sys.vo.SysDictVO>>
     * @author fuming
     * @date 2019/10/18 19:00
     */
    @PostMapping("getAdvTypeList")
    @ApiOperation("获取借款类别")
    @LogOperation("获取借款类别")
    public Result<List<SysDictVO>> getAdvTypeList() {
        List<SysDictVO> advTypeList = sysDictService.selectAdvType();
        return new Result<List<SysDictVO>>().ok(advTypeList);
    }

    @PostMapping("getInvoiceTypeList")
    @ApiOperation("获取发票种类")
    @LogOperation("获取发票种类")
    public Result<List<SysDictVO>> getInvoiceTypeList() {
        List<SysDictVO> isoCurrCd = sysDictService.getInvoiceTypeList();
        return new Result<List<SysDictVO>>().ok(isoCurrCd);
    }
}